<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Distributor extends Base{
    public function orders()
    {
    	if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('money_bill')->alias('a')->join('ordersub b', 'a.orders_id = b.id','LEFT')->join('users c', 'b.user_id = c.id','LEFT')->field('a.*,b.p_id,b.title,b.order_type,b.pay_fee,b.buy_num,c.phone')->where('a.user_id',$uid)->where('a.type',1)->order('a.id desc')->limit(10*($page-1),10)->select();
        if($res){
            foreach ($res as $key => $value) {
                $res[$key]['phone'] = substr_replace($res[$key]['phone'], '****', 3, 4);
                if($res[$key]['order_type']==1){
                    $res[$key]['img']=Db::name('product')->where('id',$res[$key]['p_id'])->value('img');
                }elseif($res[$key]['order_type']==2){
                    $res[$key]['img']=Db::name('scenic_spot')->where('id',$res[$key]['p_id'])->value('img');
                }
            }
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
    public function myTeams()
    {
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('users')->field('id,phone,headimgurl,nickname,time')->where('invite_id',$uid)->limit(10*($page-1),10)->select();
        if($res){
            foreach ($res as $key => $value) {
                $res[$key]['phone'] = substr_replace($res[$key]['phone'], '****', 3, 4);
                $res[$key]['time']=date('Y-m-d H:i:s',$res[$key]['time']);
                $res[$key]['nums']=Db::name('ordersub')->where('user_id',$res[$key]['id'])->where('state','in',[2,3,4])->count();
            }
            return json(['code' => 200, 'msg' => '成功！','page'=>$page,'data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
    public function moneyBills()
    {
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('money_bill')->where('user_id',$uid)->limit(10*($page-1),10)->select();
        if($res){
            foreach ($res as $key => $value) {
                $res[$key]['time']=date('Y-m-d H:i:s',$res[$key]['time']);
                if($res[$key]['type']==1){
                    $res[$key]['des']=Db::name('ordersub')->where('id',$res[$key]['orders_id'])->value('title');
                }elseif($res[$key]['type']==2){
                    $res[$key]['des']=Db::name('ordersub')->where('id',$res[$key]['orders_id'])->value('title');
                }elseif($res[$key]['type']==3){
                    $res[$key]['des']='提现';
                }
            }
            return json(['code' => 200, 'msg' => '成功！','page'=>$page,'data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
}